package com.project.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("scope")
public class ScopeController {

    @RequestMapping("/add")
    public String addObject(HttpServletRequest request){
        request.setAttribute("requestScope", "请求范围");

        HttpSession session = request.getSession();
        session.setAttribute("sessionScope", "会话范围");

        //得到上下文全局对象
        ServletContext context = request.getServletContext();
        context.setAttribute("contextScope", "上下文范围");
        return "/html/index.html";
    }

    @RequestMapping("/getInfo")
    @ResponseBody
    public String getObject(HttpServletRequest request){
        String str = "";
        str += "请求对象中的共享数据："+request.getAttribute("requestScope")+"<br>";
        str += "会话对象中的共享数据："+request.getSession().getAttribute("sessionScope")+"<br>";
        str += "上下文对象中的共享数据："+request.getServletContext().getAttribute("contextScope")+"<br>";

        return str;
    }
}
